
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

//获取html-webpack-plugin参数方法
var getHtmlConfig = function(name,title){
    return{
        template : "./src/"+name+".html",
        filename: "Study/"+name+".html",
        favicon: "./favicon.ico",
        title : title,
        inject : true,
        hash:true,
        chunks:["common",name] //指定引入的js模块
    }
}
//环境变量的配置dev/online
let WEBPACK_ENV = process.env.WEBPACK_ENV ||"dev";
console.log("正在运行的环境是----》"+WEBPACK_ENV);
module.exports = {
    entry : {
        "app" : ["./src/app.jsx"],
        "ReactBase" : ["./src/ReactBase.jsx"],
    },

    output: {
        path: path.resolve(__dirname, 'dist'),
        publicPath : WEBPACK_ENV ==='dev'? '/dist/': "//s.choisaaaa.shop/ampusO2O/dist/",
        filename: 'js/[name].jsx'
    },
    resolve: {
        alias : {
            page        : path.resolve(__dirname, 'src/page'),
            component   : path.resolve(__dirname, 'src/component'),
            util        : path.resolve(__dirname, 'src/util'),
            service     : path.resolve(__dirname, 'src/service')
        }
    },
    devServer :{
        port : '8086', //设置端口号
        // 路径的配置
        historyApiFallback: {
            index: '/dist/index.html'
        },
        proxy: {
            '/manage': {
                target: 'http://www.choisaaaa.campuso2o.com/',
                changeOrigin: true
            },
            '/image': {
                target: 'http://www.choisaaaa.campuso2o.com/',
                changeOrigin: true
            },
            '/user/logout.do': {
                target: 'http://www.choisaaaa.campuso2o.com/',
                changeOrigin: true
            }
        }
    },
    plugins: [
        // 处理html文件
        new HtmlWebpackPlugin({
            template: './src/index.html',
            favicon: './favicon.ico'
        }),
        new HtmlWebpackPlugin(getHtmlConfig("ReactBase","首页")),
        new ExtractTextPlugin("css/[name].css"),//CSS单独打包
        new webpack.optimize.CommonsChunkPlugin({//提出公共模块
            name : 'common',
            filename: 'js/base.js'
        }),
    ],
    module :{
        rules : [

            //css文件处理
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            //sass文件的处理
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: 'css-loader!sass-loader'
                })
            },

            //图片,字体图标配置
            {
                test: /\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        name: 'resource/[name].[ext]',
                        limit: 2000
                    }
                }]
            },
            {
                test: /\.jsx$/,
                exclude : /(node_modules)/,
                use: {
                    loader: 'babel-loader',
                    options:{
                        presets : ['env', 'react']
                    }
                }
            },


        ]
    }
};